﻿using PRO.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using PRO.Public;
using System.Data;
using PRO.Product;

namespace web.Manage.Product
{
    public partial class _default : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable cloumn = ClassLib.GetOurClassTree();
                drpClass.Items.Clear();
                for (int i = 0; i < cloumn.Rows.Count; i++)
                {
                    this.drpClass.Items.Add(new ListItem(HttpUtility.HtmlDecode(cloumn.Rows[i]["ClassName"].ToString()), cloumn.Rows[i]["id"].ToString()));
                }
                if (CVGRequest.GetQueryInt("cid", 0) > 0)
                {
                    if (drpClass.Items.FindByValue(CVGRequest.GetQueryString("cid")) != null)
                        drpClass.Items.FindByValue(CVGRequest.GetQueryString("cid")).Selected = true;
                }
                Bind();
            }
        }
        private void Bind()
        {
            int counts = 0;
            int pages = 0;

            this.Pager1.PageSize = 10;
            string where = "cid=" + this.drpClass.SelectedValue.ToInt(0);
            if (!this.txtTitle.Text.IsNullOrWhiteSpace())
                where += " and title like'%"+this.txtTitle.Text.SqlFilter()+"%'";
            switch (this.drpShow.SelectedValue.ToInt(-1))
            { 
                case 0:
                    where += " and show=0";
                    break;
                case 1:
                    where += " and show=1";
                    break;
            }
            DataTable dt = PublicLib.CutPage("Pro_Product left join Pro_Class on Pro_Product.CID=Pro_Class.id", "Pro_Product.id", "Pro_Product.edittime desc", this.Pager1.PageIndex, this.Pager1.PageSize, "Pro_Product.id,Pro_Product.cid,Pro_Product.title,Pro_Product.des,Pro_Product.icon,Pro_Product.contents,Pro_Product.price,Pro_Product.sort,Pro_Product.show,Pro_Product.edittime,Pro_Class.ClassName", where, "", out pages, out counts);
            this.Pager1.RecordCount = counts;
            this.Repeater1.DataSource = dt;
            this.Repeater1.DataBind();
        }

        protected void drpClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void drpShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void SortControl1_SortCallBack(object sender, EventArgs e)
        {
            Bind();
        }

        protected void Pager1_PageIndexChanging(object sender, PRO.Controls.Pager.NumericaArgs e)
        {
            this.Pager1.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = e.CommandArgument.ToInt(0);
            if (id > 0)
            { 
                if(!ProductLib.DelProduct(id))
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('删除失败！');</script>");
                else
                    Bind();
            }
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('参数不正确！');</script>");
           
        }
    }
}